home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / network / atre12.zip / MOSQUITO.ZIP / MOSQUITO.C < prev    next >
C/C++ Source or Header  |  1991-07-05  |  8KB  |  209 lines

  1. /*****************************************************************************
  2.  ****                                                                     ****
  3.  **** mosquito.c                                                          ****
  4.  ****                                                                     ****
  5.  **** Copyright (C) A. Dwelly and W.W. Armstrong, 1990.                   ****
  6.  ****                                                                     ****
  7.  **** All rights reserved.                                                ****
  8.  ****                                                                     ****
  9.  **** Attachment of copyright does not imply publication.                 ****
  10.  **** This file contains information which is proprietary                 ****
  11.  **** to the authors.                                                     ****
  12.  ****                                                                     ****
  13.  **** This is a test of the research version of the adaptive logic        ****
  14.  **** network package atree. This particular program demonstrates the     ****
  15.  **** algorithm's ability to extract a simple relationship from complex   ****
  16.  **** data.                                                               ****
  17.  ****                                                                     ****
  18.  **** Each entry in the training set represents a human about which       ****
  19.  **** 80 facts are known, represented by randomly setting bits. The       ****
  20.  **** function _mosquito()_ is a single bit which is set if the patient   ****
  21.  **** has been bitten by a mosquito (bit 1) is not taking quinine (bit 7) ****
  22.  **** and does not have sickle cell anemia (bit 12). The programer is free****
  23.  **** to increase the difficulty of the experiment by modifying the       ****
  24.  **** initial constants.                                                  ****
  25.  ****                                                                     ****
  26.  **** As the program is set up, the algorithm is searching in a space     ****
  27.  **** of 2^80 bits, that is, 1208925819614629174706176 seperate bit       ****
  28.  **** patterns, from this, it sees a mere 500 (a fraction of 1% of the    ****
  29.  **** possible patterns) but correctly deduces the relationship between   ****
  30.  **** the known facts, and the health of the patients.                    ****
  31.  ****                                                                     ****
  32.  **** The adaptive logic network package based on work done by Prof. W.   ****
  33.  **** W. Armstrong and others in the Department of Computing Science,     ****
  34.  **** University of Alberta, and previous work at the Universite de       ****
  35.  **** Montreal, and at AT&T Bell Laboratories, Holmdel, N. J.  The        ****
  36.  **** software demonstrates that networks consisting of many layers of    ****
  37.  **** linear threshold elements can indeed be effectively trained.        ****
  38.  ****                                                                     ****
  39.  **** License:                                                            ****
  40.  **** A royalty-free license is granted for the use of this software for  ****
  41.  **** NON_COMMERCIAL PURPOSES ONLY. The software may be copied and        ****
  42.  **** modified provided this notice appears in its entirety and unchanged ****
  43.  **** in all copies, whether changed or not.  Persons modifying the code  ****
  44.  **** are requested to state the date, the changes made and who made them ****
  45.  **** in the modification history.                                        ****
  46.  ****                                                                     ****
  47.  **** Warranty:                                                           ****
  48.  **** No warranty of any kind is provided with this software.             ****
  49.  **** This software is not supported.  Neither the authors, nor the       ****
  50.  **** University of Alberta, its officers, agents, servants or employees  ****
  51.  **** shall be liable or responsible in any way for any damage to         ****
  52.  **** property or direct personal or consequential injury of any nature   ****
  53.  **** whatsoever that may be suffered or sustained by any licensee, user  ****
  54.  **** or any other party as a consequence of the use or disposition of    ****
  55.  **** this software.                                                      ****
  56.  ****                                                                     ****
  57.  **** Patent:                                                             ****
  58.  **** The use of a digital circuit which transmits a signal indicating    ****
  59.  **** heuristic responsibility is protected by U. S. Patent 3,934,231     ****
  60.  **** and others assigned to Dendronic Decisions Limited of Edmonton,     ****
  61.  **** W. W. Armstrong, President.                                         ****
  62.  ****                                                                     ****
  63.  **** Modification history:                                               ****
  64.  ****                                                                     ****
  65.  **** 5.9.90 Initial implementation, A.Dwelly                             ****
  66.  **** 31.5.91 Windows Port & Windows Extensions, M. Thomas                ****
  67.  ****                                                                     ****
  68.  *****************************************************************************/
  69.  
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <windows.h>
  73. #include "atree.h"
  74.  
  75. #define VERBOSITY 0
  76. #define TRAINSETSIZE 500
  77. #define WIDTH 80
  78. #define TREESIZE 256
  79. #define TESTSIZE 500
  80.  
  81. #define BITTEN 1
  82. #define QUININE 7
  83. #define ANEMIA 12
  84.  
  85. #define Printf(str,fmt1,fmt2) \
  86.             { \
  87.             char Buff[80]; \
  88.             sprintf(Buff, str, fmt1, fmt2); \
  89.             MessageBox(hwnd, Buff, "Mosquito", MB_OK); \
  90.             }
  91.  
  92. char mosquito(v)
  93.  
  94. char *v;
  95.  
  96. {
  97.     return(v[BITTEN] && (!v[QUININE]) && (!v[ANEMIA]));
  98. }
  99.  
  100. BOOL NEAR PASCAL main(HWND hwnd)
  101.  
  102. {
  103.     int i;
  104.     int j;
  105.     int malaria = 0;
  106.     int healthy = 0;
  107.     char vec[WIDTH];
  108.     char ui[1];
  109.     int correct = 0;
  110.     LPBIT_VEC training_set;
  111.     LPBIT_VEC icres;
  112.     LPBIT_VEC test;
  113.     LPATREE tree;
  114.     HCURSOR hCursor;
  115.  
  116.     /* Initialise */
  117.  
  118.     training_set = (LPBIT_VEC) Malloc(TRAINSETSIZE * sizeof(bit_vec));
  119.     MEMCHECK(training_set);
  120.  
  121.     icres = (LPBIT_VEC) Malloc(TRAINSETSIZE * sizeof(bit_vec));
  122.     MEMCHECK(icres);
  123.  
  124.     atree_init();
  125.  
  126.     /* Create the test data */
  127.  
  128.     Printf("Creating training data\n",0,0);     /* Printf macro takes three arguments */
  129.  
  130.     for (i = 0; i < TRAINSETSIZE; i++)
  131.     {
  132.         /* allow multitasking during long loop! */
  133.         Windows_Interrupt(2000);
  134.  
  135.         for (j = 0; j < WIDTH; j++)
  136.         {
  137.             vec[j] = (char)RANDOM(2);
  138.         }
  139.         training_set[i] = *(bv_pack(vec, WIDTH));
  140.         ui[0] = mosquito(vec);
  141.         if (ui[0])
  142.         {
  143.             malaria++;
  144.         }
  145.         else
  146.         {
  147.             healthy++;
  148.         }
  149.         icres[i] = *(bv_pack(ui,1));
  150.     }
  151.  
  152.     Printf("There are %d patients with malaria, and %d healthy in the training set\n",malaria,healthy);
  153.  
  154.     /* Create a tree and train it */
  155.  
  156.     Printf("Training tree\n",0,0);
  157.  
  158.     tree = atree_create(WIDTH,TREESIZE);
  159.     (void) atree_train(tree,training_set,icres,0,TRAINSETSIZE,
  160.                        TRAINSETSIZE-1,10,1);
  161.  
  162.    /* Test the trained tree */
  163.  
  164.     Printf("Testing the tree\n",0,0);
  165.  
  166.     for (i = 0; i < TESTSIZE; i++)
  167.     {
  168.         /* allow multitasking during long loop! */
  169.         Windows_Interrupt(2000);
  170.  
  171.         for (j = 0; j < WIDTH; j++)
  172.         {
  173.             vec[j] = RANDOM(2);
  174.         }
  175.         test = bv_pack(vec,WIDTH);
  176.         if (atree_eval(tree,test) == mosquito(vec))
  177.         {
  178.             correct++;
  179.         }
  180.         bv_free(test);
  181.     }
  182.  
  183.     Printf("%d correct out of %d in final test\n",correct,TESTSIZE);
  184.  
  185.     /* Discard training set */
  186.  
  187.     Printf("Please wait, discarding training set",0,0);
  188.  
  189.     hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  190.     ShowCursor(TRUE);
  191.  
  192.     for (i = 0; i < TRAINSETSIZE; i++)
  193.         {
  194.         Free(training_set[i].bv);
  195.         Free(icres[i].bv);
  196.         }
  197.  
  198.     Free(training_set);
  199.     Free(icres);
  200.  
  201.     ShowCursor(FALSE);
  202.     SetCursor(hCursor);
  203.  
  204.     /* Discard tree */
  205.     atree_free(tree);
  206.  
  207.     return(1);
  208. }
  209.